package com.bqnxzz.ywiigame;

import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.util.Timer;
import java.util.TimerTask;

public class PreloadingProxy extends TimerTask implements Runnable {
    ViewGroup rootLayer;
    private ProgressBar progressBar;
    private TextView textView;
    public TextView versionTf;

    public float percent;
    public Timer timer;
    private boolean timerRuned = false;

    PreloadingProxy(ViewGroup rootLayer, ProgressBar progressBar, TextView textView, TextView versionTf) {
        this.rootLayer = rootLayer;
        this.progressBar = progressBar;
        this.textView = textView;
        this.versionTf = versionTf;

        timer = new Timer();
        percent = 0;

        timer.schedule(this, 0L, 1000L / 6L);
    }

    private void stopInit() {
        timerRuned = false;
        percent = 0;
    }

    public void startInit() {
        stopInit();
        timerRuned = true;
    }

    void dispose() {
        this.cancel();
        timer.purge();
        timer.cancel();
    }

    private void egretMiniAppLoadingProgress(final String msg, int val) {
        val = Math.max(0, val);
        val = Math.min(100, val);

        final int vl = val;
        MainActivity.self.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                progressBar.setProgress(vl);

                String infoMsg = msg + " [" + vl + "%]";
                textView.setText(infoMsg);
            }
        });
    }

    public void updateInfo(final String infoMsg) {
        MainActivity.self.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                stopInit();
                textView.setText(infoMsg);
            }
        });
    }

    public void stopAndError(final String msg) {
        MainActivity.self.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                stopInit();
                progressBar.setProgress(0);

                String infoMsg = msg + "";
                textView.setText(infoMsg);
            }
        });
    }

    public void fullAndInfo(final String msg) {
        MainActivity.self.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                stopInit();

                progressBar.setProgress(100);

                String infoMsg = msg + "";
                textView.setText(infoMsg);
            }
        });
    }

    public void updateNowVersion(final String version) {
//        MainActivity.self.runOnUiThread(new Runnable() {
//            @Override
//            public void run() {
//                stopInit();
//
//                versionTf.setText(version);
//            }
//        });
    }

    public void loadPatch(final String msg, final int val) {
        MainActivity.self.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                stopInit();
                egretMiniAppLoadingProgress(msg, val);
            }
        });
    }

    public void unzipProgress(final int val) {
        MainActivity.self.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                stopInit();
                egretMiniAppLoadingProgress("Decompress resources for the first time (this process does not consume traffic)", val);
            }
        });
    }

    @Override
    public void run() {
        if (!timerRuned) {
            return;
        }

        MainActivity.self.runOnUiThread(() -> egretMiniAppLoadingProgress("Loading", (int) percent));
        percent += 1f;
        percent = Math.min(99, percent);
    }
}
